﻿using System;
using RayD.Repository.Entites.SystemModule;
using System.Collections.Generic;
using System.Text;
using RayD.Repository;
using RayD.Core;
using Dapper;
using System.Data;
using System.Linq;
using Microsoft.EntityFrameworkCore.Internal;

namespace RayD.App.SystemModule
{
    public class MonitorApp
    {
        #region 构造函数注入
        private IRepository BaseRepository;
        public MonitorApp(IRepository repository)
        {
            this.BaseRepository = repository;
        }
        #endregion

        #region 获取数据
        /// <summary>
        /// 获取本期调度数据
        /// </summary>
        /// <returns></returns>
        public DataTable GetApiByTimeType(TimeType timeType)
        {
            //返回DataTable
            DataTable dt = new DataTable();
            dt.Columns.Add("时期", typeof(string));
            dt.Columns.Add("调度次数", typeof(int));
            //查询源数据
            DateTime now = DateTime.Now.ToCstTime();
            var expression = LinqExtensions.True<LogEntity>()
                .And(c => c.B_OperateTime >= now.GetTimeStartByType(timeType))
                .And(c => c.B_OperateTime < now.GetTimeEndByType(timeType));
            var groups = this.BaseRepository.IQueryable(expression)
                .OrderBy(c => c.B_OperateTime)
                .Select(c => new { period = c.B_OperateTime.ToDate().FormatTimeByType(timeType) })
                .GroupBy(c => c.period);
            foreach (var group in groups)
            {
                DataRow row = dt.NewRow();
                row["时期"] = group.Key;
                row["调度次数"] = group.Count();
                dt.Rows.Add(row);
            }
            return dt;
        }
        #endregion
    }
}
